class Solution {
public:
    int lengthOfLongestSubstring(string s) {

        int n = s.size();

        if (n == 0) return 0;

        unordered_set<int> myset;

        myset.insert(s[0]);

        int ans = 1;

        for (int i = 1, j = 0; i < n; i++)
        {
            char ch = s[i];

            while (myset.count(ch) > 0)
            {
                myset.erase(s[j]);
                j++;
            }
            myset.insert(ch);

            ans = max(ans, i - j + 1);
        }

        return ans;
    }
};